<?php

namespace App\Callback;

class ProcessSale
{
    private $callbacks = [];

    public function RegisterCallback($callback)
    {
        if (!is_callable($callback)) {
            throw new UnCallbackException('不可执行的回调函数');
        }
        $this->callbacks[] = $callback;
    }

    public function sale($product)
    {
        print "{$product->name}: processing\n";
        foreach ($this->callbacks as $callback) {
            call_user_func($callback, $product);
        }
        print "{$product->name}: processed\n";
    }
}
